<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.serviceName"
          placeholder="请输入服务名称"
          maxlength="20"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.status"
          placeholder="请选择状态"
        >
          <el-option label="启用" value="0"></el-option>
          <el-option label="停用" value="2"></el-option>
          <el-option label="到期" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加服务</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
          fixed
        ></el-table-column>
        <el-table-column
          prop="serviceName"
          label="服务名称"
          width="320"
          fixed="left"
        ></el-table-column>
        <el-table-column
          prop="serviceNo"
          label="服务编号"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="appKey"
          label="服务Key"
          width="240"
        ></el-table-column>
        <el-table-column
          prop="appSecret"
          label="服务Secret"
          width="400"
        ></el-table-column>
        <el-table-column
          prop="tradeType"
          label="所属行业"
          width="400"
        ></el-table-column>
        <el-table-column prop="name" label="服务起止时间" width="200">
          <template slot-scope="scope">
            <span v-if="scope.row.startTime == '' && scope.row.stopTime == ''"
              >永久有效</span
            >
            <span v-else>{{
              scope.row.startTime + ' 至 ' + scope.row.stopTime
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="addTime"
          label="创建时间"
          width="200"
        ></el-table-column>
        <el-table-column prop="serviceDesc" width="340px" label="服务描述">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.serviceDesc"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.serviceDesc }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="80px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.status == 0"
              >启用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.status == 2"
              >停用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.status == 1"
              >到期</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 2 && $checkPermission(105)"
                  >启用</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 0 && $checkPermission(106)"
                  >停用</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
                <el-dropdown-item
                  command="authorization"
                  v-if="$checkPermission(112)"
                  >接口授权</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="100px" :model="form" size="medium">
        <el-form-item label="服务名称" required>
          <el-input
            v-model.trim="form.serviceName"
            clearable
            maxlength="20"
            placeholder="请输入服务名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属行业" required>
          <el-select
            clearable
            multiple
            v-model="form.tradeType"
            placeholder="请选择所属行业"
            style="width: 100%"
          >
            <el-option
              v-for="item in typeData"
              :key="item.dicLabel"
              :label="item.dicLabel"
              :value="item.dicValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属应用" required>
          <el-select
            clearable
            v-model="form.openAppId"
            placeholder="请选择所属应用"
            style="width: 100%"
          >
            <el-option
              v-for="item in appData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起止时间">
          <el-date-picker
            v-model="dateRange"
            @change="dateChange(dateRange)"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="白名单IP">
          <el-tag
            v-for="(tag, index) in form.ip"
            :key="index"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)"
          >
            {{ tag }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            v-model.trim="inputValue"
            ref="saveTagInput"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button v-else size="small" @click="showInput"
            >+ 添加 IP</el-button
          >
        </el-form-item>
        <el-form-item label="白名单状态">
          <el-select clearable v-model="form.ipCheckEnable" style="width: 100%">
            <el-option label="开启" value="1"></el-option>
            <el-option label="关闭" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务描述">
          <el-input
            type="textarea"
            v-model.trim="form.serviceDesc"
            placeholder="请输入服务描述"
            maxlength="200"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false">取消</span>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="服务名称">
          {{ lockData.serviceName }}
        </el-descriptions-item>
        <el-descriptions-item label="服务编号">
          {{ lockData.serviceNo }}
        </el-descriptions-item>
        <el-descriptions-item label="服务Key">
          {{ lockData.appKey }}
        </el-descriptions-item>
        <el-descriptions-item label="服务Secret">
          {{ lockData.appSecret }}
        </el-descriptions-item>
        <el-descriptions-item label="所属行业" :span="2">
          {{ lockData.tradeType }}
        </el-descriptions-item>
        <el-descriptions-item label="服务起止时间" :span="2">
          <i v-if="lockData.startTime == '' && lockData.stopTime == ''">
            永久有效
          </i>
          <i v-else>{{ lockData.startTime + ' 至 ' + lockData.stopTime }}</i>
        </el-descriptions-item>
        <el-descriptions-item label="白名单状态">
          {{ lockData.ipCheckEnable == 1 ? '开启' : '关闭' }}
        </el-descriptions-item>
        <el-descriptions-item label="所属应用">
          {{ lockData.openAppName }}
        </el-descriptions-item>
        <el-descriptions-item label="白名单IP" :span="2">
          {{ lockData.ip.join(',') }}
        </el-descriptions-item>
        <el-descriptions-item label="服务描述" :span="2">
          <el-tooltip
            popper-class="ellipsis-tip-span2"
            :content="lockData.serviceDesc"
            placement="top-start"
          >
            <span class="ellipsis-span2">{{ lockData.serviceDesc }}</span>
          </el-tooltip>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      typeData: [],
      appData: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      title: '添加服务',
      form: {},
      dateRange: [],
      ip: '',
      inputVisible: false,
      inputValue: '',
      isLook: false,
      lockData: { ip: [] },
    }
  },
  created() {
    this.typeList()
    this.appList()
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios.post('/admin/service/page', this.formSearch).then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data.list
          this.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    dateChange(val) {
      // 时间范围发生变化
      if (val == null) {
        this.form.startTime = ''
        this.form.stopTime = ''
        return
      }
      this.form.startTime = val[0]
      this.form.stopTime = val[1]
    },
    add() {
      //添加
      this.title = '添加服务'
      this.dateRange = []
      this.form = { status: '0', ip: [], ipCheckEnable: '2' }
      this.visible = true
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$axios
            .get('/admin/service/find?flag=false&id=' + row.id)
            .then((data) => {
              this.lockData = { ip: [] }
              if (data.code == 200) {
                this.lockData = data.data
                this.isLook = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'edit':
          this.title = '编辑服务'
          this.$axios
            .get('/admin/service/find?flag=true&id=' + row.id)
            .then((data) => {
              this.form = { status: '0', ip: [], ipCheckEnable: '2' }
              if (data.code == 200) {
                this.form = data.data
                this.form.tradeType = data.data.tradeType.split(',')
                if (data.data.startTime) {
                  this.dateRange = [data.data.startTime, data.data.stopTime]
                }
                this.form.status = String(data.data.status)
                this.visible = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'changeStatus':
          //启用停用
          const status = row.status == 0 ? 2 : 0
          this.$axios
            .get('/admin/service/start?id=' + row.id + '&status=' + status)
            .then((data) => {
              if (data.code == 200) {
                this.itemList()
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios
                .get('/admin/service/del?id=' + row.id)
                .then((data) => {
                  if (data.code == 200) {
                    this.itemList()
                    this.$message.success('操作成功！')
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            })
            .catch(() => {})
          break
        case 'authorization':
          //接口授权
          this.$router.push({
            path: '/authenticationCenter/authorization',
            query: { serviceId: row.id },
          })
          break
      }
    },
    save() {
      //保存
      if (!this.form.serviceName) {
        this.$message.error('请输入服务名称')
        return
      }
      if (!this.form.tradeType) {
        this.$message.error('请选择所属行业')
        return
      }
      this.form.tradeType = this.form.tradeType.join(',')
      this.$axios.post('/admin/service/edit', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    handleClose(tag) {
      this.form.ip.splice(this.form.ip.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      let inputValue = this.inputValue
      if (inputValue) {
        this.form.ip.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    typeList() {
      //获取所属类型
      this.$axios.get('/admin/dic/listDic?type=trade_type').then((data) => {
        this.typeData = []
        if (data.code == 200) {
          this.typeData = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    appList() {
      //获取所属应用
      this.$axios.post('/admin/app/list').then((data) => {
        this.appData = []
        if (data.code == 200) {
          this.appData = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
  },
}
</script>
<style>
.el-tag {
  margin-right: 10px;
}
</style>
